package services

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"mall_frontend_api/biz/dal"
	"mall_frontend_api/biz/handler/base"
	"mall_frontend_api/biz/model/category"
	"mall_frontend_api/biz/pack"
)

type CategoryService struct {
	ctx    context.Context
	common *base.Response
	dao    *dal.InitDAO
}

func NewCategoryService(ctx context.Context, common *base.Response) *CategoryService {
	return &CategoryService{
		ctx:    ctx,
		common: common,
		dao:    dal.NewInitDAO(),
	}
}

func (c *CategoryService) GetList(req *category.GetCategoryListRequest) ([]*category.Category, error) {
	list, err := c.dao.CategoryDAO.GetList(c.ctx, []gen.Condition{}, 0, 0)
	if err != nil {
		hlog.CtxErrorf(c.ctx, "CategoryDAO.GetCategoryListWithTotal error: %v", err)
		return nil, err
	}

	return pack.GetCategoryList(list, 0), nil
}
